﻿using MugenMvvmToolkit.Interfaces;
using MugenMvvmToolkit.Interfaces.Views;
using MugenMvvmToolkit.Utils;
#if !NETFX_CORE
using System.Windows;
using System.Windows.Controls;
#else
using Windows.UI.Xaml.Controls;
#endif

namespace MugenMvvmToolkit.Infrastructure
{
    /// <summary>
    ///     Manages states and the logic for transitioning between states for controls.
    /// </summary>
    public class VisualStateManager : IVisualStateManager
    {
        #region Fields

        private readonly IThreadManager _threadManager;

        #endregion

        #region Constructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="VisualStateManager" /> class.
        /// </summary>
        public VisualStateManager(IThreadManager threadManager)
        {
            _threadManager = threadManager;
        }

        #endregion

        #region Implementation of IVisualStateManager

        /// <summary>
        ///     Transitions the control between two states.
        /// </summary>
        /// <returns>
        ///     true if the control successfully transitioned to the new state; otherwise, false.
        /// </returns>
        /// <param name="view">The view to transition between states. </param>
        /// <param name="stateName">The state to transition to.</param>
        /// <param name="useTransitions">true to use a VisualTransition to transition between states; otherwise, false.</param>
        public void GoToState(IView view, string stateName, bool useTransitions)
        {
            Should.NotBeNull(view, "view");
            _threadManager.InvokeInUi(() =>
                                          {
#if WPF
                                              System.Windows.VisualStateManager.GoToState((FrameworkElement) view,
                                                                                          stateName, useTransitions);
#else
#if !NETFX_CORE
                                              System.Windows.VisualStateManager.GoToState((Control)view, stateName, useTransitions);
#else
                                              Windows.UI.Xaml.VisualStateManager.GoToState((Control)view, stateName, useTransitions);
#endif

#endif
                                          });
        }

        #endregion
    }
}